﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using DeSleeper.Client.WakeUp;
using MbUnit.Framework;
using DeSleeper.Service;
using MbUnit.Framework.Xml;

namespace DeSleeper.Tests
{
	[TestFixture]
	public class SerializationFixture
	{
		[Test]
		public void SerializeHostInfo()
		{
			var expected = new HostInfo
			               	{
			               		Current = new IPAddress(new byte[] {192, 168, 0, 1}),
			               		Subnet = new IPAddress(new byte[] {255, 255, 255, 0}),
			               		MacAddress = new PhysicalAddress(new byte[] {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB})
			               	};
			var result = TestSerialization(expected);
			Assert.AreEqual(expected, result);
		}

		[Test]
		public void SerializeDestinationCache()
		{

			var cache = new Dictionary<string, HostInfo>
			            	{{"Test", new HostInfo
			            	          	{
			            	          		Current = IPAddress.Parse("192.168.0.1"),
			            	          		Subnet = IPAddress.Parse("255.255.255.0"),
			            	          		MacAddress = PhysicalAddress.Parse("0123456789AB")
			            	          	}
			            		},
			            		{"Test2", new HostInfo
			            		          	{
			            		          		Current = IPAddress.Parse("192.168.0.4"),
			            		          		Subnet = IPAddress.Parse("255.255.255.128"),
			            		          		MacAddress = PhysicalAddress.Parse("123456789ABC")
			            		          	}}
			            	};
			var expected = new TargetCache(cache);
			var result = TestSerialization(expected);
			Assert.AreEqual(expected, result);
            XmlSerializationAssert.AreEqualAfterRoundTrip(expected);
		}

        [Test]
        public void SerializeBlankTargetComputer()
        {
            var expected = new TargetComputer
                               {
                                   Description = "Test"
                               };

            var result = TestSerialization(expected);
            Assert.AreEqual(expected.Description, result.Description);
        }


		private static T TestSerialization<T>(T obj)
			where T : class 
		{
			var serializer = new XmlSerializer(obj.GetType());
			using (var stream = new MemoryStream())
			{
				using (var writer = XmlWriter.Create(stream))
				{
					writer.WriteStartElement("TestElement");
					serializer.Serialize(writer, obj);
					writer.WriteEndElement();
				}

				stream.Seek(0, SeekOrigin.Begin);
				using (var reader = new StreamReader(stream))
				{
					Console.WriteLine(reader.ReadToEnd());
					stream.Seek(0, SeekOrigin.Begin);
					using (var xmlReader = XmlReader.Create(stream))
					{
						xmlReader.ReadToFollowing("TestElement");
						xmlReader.ReadStartElement("TestElement");
						var output  = serializer.Deserialize(xmlReader);
						xmlReader.ReadEndElement();
						return output as T;
					}
				}
			}
		}
	}
}